//
//  KTInputViewTableCell.m
//  BoWuGuan
//
//  Created by 梁尚嘉 on 2016/11/3.
//  Copyright © 2016年 KT. All rights reserved.
//

#import "KTInputViewTableCell.h"
@interface KTInputViewTableCell () <UITextViewDelegate>
@property (nonatomic, strong) UITextView *textView;
@property (nonatomic, strong) UIButton *maxLengthButton;
@end

@implementation KTInputViewTableCell
@synthesize hasText = _hasText;

- (void)setPlaceholder:(NSString *)placeholder
{
    _placeholder = placeholder;
    self.elementLabel.text = placeholder;
}

- (void)setPlaceholderColor:(UIColor *)placeholderColor
{
    _placeholderColor = placeholderColor;
    self.elementLabel.textColor = placeholderColor;
}

- (void)configure
{
    [super configure];

    self.placeholderColor = HEXCOLOR(@"#C1C1C1");
    self.maxLength = 50;
    [self.elementLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(SM_SIZE(30)));
        make.top.equalTo(@(SM_SIZE(25)));
    }];

    [self.contentView addSubview:self.textView];
    [self.textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@(SM_SIZE(12)));
        make.left.equalTo(@(SM_SIZE(20)));
        make.right.equalTo(@(SM_SIZE(-20)));
        make.bottom.equalTo(@(SM_SIZE(-100)));
    }];

    _maxLengthButton = [UIButton new];
    _maxLengthButton.enabled = NO;
    _maxLengthButton.contentEdgeInsets = UIEdgeInsetsMake(SM_SIZE(10), SM_SIZE(10), SM_SIZE(10), SM_SIZE(10));
    _maxLengthButton.layer.cornerRadius = SM_SIZE(5);
    _maxLengthButton.clipsToBounds = true;
    _maxLengthButton.titleLabel.numberOfLines = 1;
    _maxLengthButton.titleLabel.lineBreakMode = NSLineBreakByCharWrapping;
    [_maxLengthButton setTitleColor:HEXCOLOR(@"#989999") forState:UIControlStateNormal];
    _maxLengthButton.titleLabel.font = SM_CFont(28);
    _maxLengthButton.backgroundColor = [[UIColor lightGrayColor] colorWithAlphaComponent:0.7f];
    [self.contentView addSubview:_maxLengthButton];
    [self.maxLengthButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.textView.mas_right).offset(SM_SIZE(-15));
        make.height.equalTo(@(SM_SIZE(60)));
        make.bottom.equalTo(@(SM_SIZE(-20)));
        make.width.greaterThanOrEqualTo(@(SM_SIZE(150)));
    }];

    @weakify(self);
    [self.textView.rac_textSignal subscribeNext:^(id x) {
        @strongify(self);
        self.hasText = self.textView.text.length;
        if (self.textView.text.length>self.maxLength) {
            self.textView.text = [self.textView.text substringWithRange:NSMakeRange(0, self.maxLength)];
        }
        [self updateMaxLength];
    }];

}

- (void)updateMaxLength
{
    [self.maxLengthButton setTitle:[NSString stringWithFormat:@"%ld/%ld字",self.textView.text.length,self.maxLength] forState:UIControlStateNormal];
}

#pragma mark - Getters

- (UITextView *)textView
{
    if (!_textView) {
        _textView = [[UITextView alloc]init];
        _textView.font = kElementTableCellFont;
        _textView.backgroundColor = nil;
    }
    return _textView;
}

- (NSString *)text
{
    return _textView.text;
}

- (BOOL)hasText
{
    return _hasText;
}


#pragma mark - Setters
- (void)setMaxLength:(NSUInteger)maxLength
{
    _maxLength = maxLength;
    [self updateMaxLength];
}

- (void)setText:(NSString *)text
{
    _textView.text = text;
}


- (void)setHasText:(BOOL)hasText
{
    _hasText = hasText;
    self.elementLabel.hidden = hasText;
}


+ (CGFloat)cellHeight
{
    return SM_SIZE(270);
}

@end
